package com.skit.mine.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.skit.common.ResourcesManager;
import com.skit.mine.R;

import java.util.List;

/**
 * @author shuike <a href="mailto:shuike007@gmail.com">Contact me.</a>
 * @version 1.0
 * @since 2020/5/23
 */
public class InfoListAdapter extends RecyclerView.Adapter<InfoListAdapter.ViewHolder> {
    private List<String[]> list;
    private OnItemClickListener onItemClickListener;

    public static interface OnItemClickListener {
        void onItemClickListener(View view, int position);
    }

    public InfoListAdapter(List<String[]> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_info_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.title.setText(list.get(position)[0]);
        String content = list.get(position)[1];
        holder.content.setText(content);
        if ("未设置".equals(content)) {
            holder.content.setTextColor(ResourcesManager.getRes().getColor(R.color.textGray));
        } else {
            holder.content.setTextColor(ResourcesManager.getRes().getColor(R.color.black));
        }
        holder.content.setOnClickListener(v -> {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClickListener(v, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView content, title;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title);
            content = itemView.findViewById(R.id.content);
        }
    }
}
